//
//  Bounds.m
//  CompGraf2
//
//  Created by Juan Niosi on 02/09/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Bounds.h"
#import "Entity.h"

@implementation Bounds
@synthesize xMin, xMax, yMin, yMax, zMin, zMax;

- (id)init
{
    self = [super init];
    if (self) {
        xMin = NAN;
        xMax = NAN;
        yMin = NAN;
        yMax = NAN;
        zMin = NAN;
        zMax = NAN;
    }
    
    return self;
}

- (id)initWithValues:(GLfloat)xm :(GLfloat)xM :(GLfloat)ym :(GLfloat)yM :(GLfloat)zm :(GLfloat)zM
{
    self = [super init];
    if (self) {
        self.xMin = xm;
        self.xMax = xM;
        
        self.yMin = ym;
        self.yMax = yM;
        
        self.zMin = zm;
        self.zMax = zM;
    }
    
    return self;
}

- (id)initWithEntity:(Entity *)entity
{
    self = [super init];
    if (self) {
        int i = 0;
        if (entity.vertCount == 0)
        {
            xMin = 0;
            xMax = 0;
            yMin = 0;
            yMax = 0;
            zMin = 0;
            zMax = 0;
            return self;
        }
        
        xMin = entity.vertices[i];
        xMax = entity.vertices[i];
        i++;
        
        yMin = entity.vertices[i];
        yMax = entity.vertices[i];
        i++;
        
        zMin = entity.vertices[i];
        zMax = entity.vertices[i];
        i++;
        
        while ( i < entity.vertCount * 3)
        {
            if (entity.vertices[i] < xMin)
                xMin = entity.vertices[i];
            else if (entity.vertices[i] > xMax)
                xMax = entity.vertices[i];
            i++;
            
            if (entity.vertices[i] < yMin)
                yMin = entity.vertices[i];
            else if (entity.vertices[i] > yMax)
                yMax = entity.vertices[i];
            i++;
            
            if (entity.vertices[i] < zMin)
                zMin = entity.vertices[i];
            else if (entity.vertices[i] > zMax)
                zMax = entity.vertices[i];
            i++;
        }
        
        for (Entity *child in entity.children)
        {
            if (child.bounds.xMin < xMin)
                xMin = child.bounds.xMin;
            
            if (child.bounds.xMax > xMax)
                xMax = child.bounds.xMax;
            
            if (child.bounds.yMin < yMin)
                yMin = child.bounds.yMin;
            
            if (child.bounds.yMax > yMax)
                yMax = child.bounds.yMax;
            
            if (child.bounds.zMin < zMin)
                zMin = child.bounds.zMin;
            
            if (child.bounds.zMax > zMax)
                zMax = child.bounds.zMax;
        }
    }
    
    return self;
}

- (void)draw:(Engine *)engine
{
    [engine drawBox:xMin :xMax :yMin :yMax :zMin :zMax];
}

- (GLfloat)getWidth
{
    return xMax - xMin;
}

- (GLfloat)getHeight
{
    return yMax - yMin;
}

- (GLfloat)getDepth
{
    return zMax - zMin;
}

- (NSString *)toString
{
    return [[NSString alloc] initWithFormat:@"xMin:%f xMax:%f yMin:%f yMax%f zMin:%f zMax:%f",xMin,xMax,yMin,yMax,zMin,zMax];
}

- (BOOL)pointInside:(Vector3 *)point
{
    if ([point getX] <= xMin || [point getX] >= xMax
//        || [point getY] < yMin || [point getY] > yMax //No necesario
        || [point getZ] <= zMin || [point getZ] >= zMax)
        return false;
    return true;
}

- (void)dealloc
{
    [super dealloc];
}
@end
